package it.polimi.TravelDream.ejb.searchManager;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import it.polimi.TravelDream.ejb.dtos.FlightDTO;
import it.polimi.TravelDream.ejb.eaos.FlightSearch;
import it.polimi.TravelDream.ejb.entities.Flight;
import it.polimi.TravelDream.ejb.entities.Group;

import javax.annotation.security.RolesAllowed;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class FlightManager
 */
@Stateless
@LocalBean
public class FlightManager implements FlightSearch{
	
	@PersistenceContext
    private EntityManager em;
	private Date departureDate;
	private String departureCity;
	private String departureAirport;
	private String arrivalAirport;
	private String arrivalCity;
	private List <Flight> result = new ArrayList <Flight>();
	
    /**
     * Default constructor. 
     */
    public FlightManager() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public List <FlightDTO> getResultList(){
    	
    	List <FlightDTO> FlightDTOs = new ArrayList <FlightDTO>();
    	
    	for (Flight f:this.result){
    		FlightDTO flightDTO = convertToDTO(f);
    		FlightDTOs.add(flightDTO);
    	}
    	return FlightDTOs;
    }
  
    @Override
    @RolesAllowed({Group._ADMIN})
    public void search(){
    	this.result = em.createNamedQuery(Flight.SEARCH_FLIGHT, Flight.class).setParameter("departureDate", this.departureDate)
    			.setParameter("departureCity", this.departureCity).setParameter("departureAirport", this.departureAirport)
    			.setParameter("arrivalCity" , this.arrivalCity).setParameter("arrivalAirport" , this.arrivalAirport).getResultList();
    } 
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public Date getDepartureDate() {
		return departureDate;
	}
    
    @Override
    @RolesAllowed({Group._ADMIN})
	public void setDepartureDate(Date departureDate) {
		this.departureDate = departureDate;
	}
 
    @Override
    @RolesAllowed({Group._ADMIN})
	public String getDepartureCity() {
		return departureCity;
	}
	
	@Override
	@RolesAllowed({Group._ADMIN})
	public void setDepartureCity(String departureCity) {
		this.departureCity = departureCity;
	}

	@Override
	@RolesAllowed({Group._ADMIN})
	public String getDepartureAirport() {
		return departureAirport;
	}

	@Override
	@RolesAllowed({Group._ADMIN})
	public void setDepartureAirport(String departureAirport) {
		this.departureAirport = departureAirport;
	}
	
	
	@Override
	@RolesAllowed({Group._ADMIN})
	public String getArrivalAirport() {
		return arrivalAirport;
	}
	
	@Override
	@RolesAllowed({Group._ADMIN})
	public void setArrivalAirport(String arrivalAirport) {
		this.arrivalAirport = arrivalAirport;
	}
	
	@Override
	@RolesAllowed({Group._ADMIN})
	public String getArrivalCity() {
		return arrivalCity;
	}
	
	@Override
	@RolesAllowed({Group._ADMIN})
	public void setArrivalCity(String arrivalCity) {
		this.arrivalCity = arrivalCity;
	}

	private FlightDTO convertToDTO (Flight flight){
    	FlightDTO flightDTO = new FlightDTO();
    	flightDTO.setArrivalAirport(flight.getArrivalAirport());
    	flightDTO.setArrivalCity(flight.getArrivalCity());
    	flightDTO.setDepartureAirport(flight.getDepartureAirport());
    	flightDTO.setDepartureCity(flight.getDepartureCity());
    	flightDTO.setDepartureDate(flight.getDepartureDate());
    	flightDTO.setPrice(flight.getPrice());
    	flightDTO.setSeat(flight.getSeat());
    	return flightDTO;
    }

	@Override
	@RolesAllowed({Group._ADMIN})
	public void searchAll() {
		this.result = em.createNamedQuery(Flight.FIND_ALL_FLIGHT, Flight.class).getResultList();
	}

	@Override
	@RolesAllowed({Group._ADMIN})
	public void update(FlightDTO flightDTO, int index) {
		Flight flight = new Flight();
		
		flight = copyValuesFromDTO(flightDTO, index);
		
		em.merge(flight);
	}
	
	private Flight copyValuesFromDTO (FlightDTO flightDTO, int index) {
		Flight flight = new Flight();
		
		flight = result.get(index);
		
    	flight.setArrivalAirport(flightDTO.getArrivalAirport());
    	flight.setArrivalCity(flightDTO.getArrivalCity());
    	flight.setDepartureAirport(flightDTO.getDepartureAirport());
    	flight.setDepartureCity(flightDTO.getDepartureCity());
    	flight.setDepartureDate(flightDTO.getDepartureDate());
    	flight.setPrice(flightDTO.getPrice());
    	flight.setSeat(flightDTO.getSeat());
    	return flight;
	}
  
}
